// pages/login/index.js
var api = require('../../config/api.js');
var util = require('../../utils/util.js');

var app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    appUserInfo: {
      userName: '',
    },
    userInfoList: [],
    isLogin: false,
    loginType: 1
  },
  onChange(event) {
    this.setData({
      checked: event.detail,
    });
  },
  //微信手机号登录
  getPhone(options) {
    wx.login({
      success: (res) => {
        wx.request({
          url: api.AuthLoginByWeixin,
          data: {
            iv: options.detail.iv,
            encryptedData: options.detail.encryptedData,
            code: res.code
          },
          method: 'POST',
          header: {
            'content-type': 'application/json'
          },
          success: function(res) {
            if (res.data.result == 1) {
              wx.setStorageSync('isLogin', true);
              wx.setStorageSync('appUserInfo', res.data.info.appUserInfo);
              wx.setStorageSync('token', res.data.info.accessToken);
              wx.setStorageSync('userInfoList', res.data.info.userInfoList);
              wx.switchTab({
                url: '/pages/index/index'
              });
            } else {
              wx.setStorageSync('isLogin', false);
                util.showErrorToast(res.data.msg);
            }
          }
        });
      }
    })
  },

  loginByPhone () {
    this.setData({
      loginType: 2
    });
  },
  changePhone (e) {
    const phone = e.detail;
    this.setData({ phone });
    const phoneReg = /^1[3456789]\d{9}$/;
    if (phoneReg.test(phone)) {
      this.setData({
        verify: true
      });
    } else {
      this.setData({
        verify: false
      });
    }
  },
  changeCode (e) {
    this.setData({ code: e.detail });
  },
  sendVerifyCode () {
    const { verify, timeId } = this.data;
    if (!verify || timeId !== null) return;
    let startTime = 59;
    const intervalId = setInterval(() => {
      if (startTime <= 1) {
        clearInterval(intervalId);
        this.setData({
          timeId: null,
          verifyCount: 59
        });
        return;
      }
      startTime--;
      this.setData({
        verifyCount: startTime
      });
    }, 1000);
    this.setData({
      timeId: intervalId
    });
  },
  login () {
    const { checked, phone, code } = this.data;
    if (!phone) {
      wx.showToast({
        title: '请输入手机号',
        icon: 'none'
      });
      return;
    }
    if (!code) {
      wx.showToast({
        title: '请输入验证码',
        icon: 'none'
      });
      return;
    }
    if (!checked) {
      wx.showToast({
        title: '请先同意协议',
        icon: 'none'
      });
      return;
    }
    const params = {
      phone,
      code
    };
    // TODO:请求接口
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.login({
      success(res) {
        console.log(res)
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if (wx.getStorageSync('isLogin') == true) {
      let appUserInfo = wx.getStorageSync('appUserInfo');
      this.setData({
        isLogin: true,
        appUserInfo: appUserInfo,
        userInfoList: wx.getStorageSync('userInfoList')
      });
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    const { timeId } = this.data;
    clearInterval(timeId);
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})